#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "game.h"

static void formhead(void);
static void formtail(void);
static void menu_printf(void);
static void game_printf(int min, int max);
static void over_printf(int num);
static void login_menu_printf(void);
static void write_account(struct user user_data);

void start(void)
{
	system("clear");
	formhead();
	printf(GREEN"\n\n\n\t\t 欢迎来到猜数字游戏！\n\n\n\n"TAIL);
	printf(GREEN"\t\t 轻触回车键进入菜单 \n\n\n"TAIL);
	formtail();
	getchar();
	setbuf(stdin, NULL);
}


int login_menu(void)
{
	int log_on_choice;
	char ch;

	system("clear");
	formhead();
	login_menu_printf();
	formtail();

	while (1) {
		scanf("%d",&log_on_choice);
		while ((ch = getchar()) != '\n' && ch != EOF);

		if (log_on_choice != 1 && log_on_choice != 2 && log_on_choice != 3) {
			system("clear");
			formhead();
			printf(RED"\n\t\t唔岩呃！茂利！\n"TAIL);
			login_menu_printf();
			formtail();
		}
		else
			break;
	}

	return log_on_choice;
}


void register_account(struct user list[])
{
	struct user user_data;
	char account_temp[10];
	char password_temp[10];
	int time;

	printf(PURPLE"帐号（限10位）:"TAIL);
	scanf("%s", account_temp);
	strcpy(user_data.account, account_temp);
	for (time = 0; time < USER_MAX; time++) {
		if (strcmp(list[time].account, account_temp)  == 0){
			system("clear");
			formhead();
			printf("\t\t帐号已被注册，轻触回车返回上一步\n");
			formtail();
			getchar();
			setbuf(stdin, NULL);
			getchar();
			return;
		}
	}

	printf(PURPLE"密码（限10位）:"TAIL);
	scanf("%s", password_temp);
	strcpy(user_data.password, password_temp);
	write_account(user_data);
}


struct user login(void)
{
	struct user login_data;

	char account_temp[10];
	char password_temp[10];

	printf(PURPLE"帐号（限10位）:"TAIL);
	scanf("%s", account_temp);
	strcpy(login_data.account, account_temp);
	memcpy(password_temp, getpass(PURPLE"密码（限10位）:"TAIL),10);
	strcpy(login_data.password, password_temp);

	return login_data;
}


int exist(struct user list[], struct user user_login)
{
	int time;
	for (time = 0; time < USER_MAX; time++)
	{
		if(strcmp(list[time].account, user_login.account) == 0 && strcmp(list[time].password, user_login.password) == 0) {
			return 1;
		}
	}
	return -1;
}


/*input'1',fast game.    input'2',general game.
  input'3',ruler.        input'4',quit.*/

int menu(void)
{
	int choice;
	char ch;	
	
	system("clear");
	formhead();
	menu_printf();
	formtail();
	
	while (1) {
		scanf("%d",&choice);
		while ((ch = getchar()) != '\n' && ch != EOF);

		if (choice != 1 && choice != 2 && choice != 3 && choice != 4) {
			system("clear");
			formhead();
			printf(RED"\n\t\t唔岩呃！茂利！\n"TAIL);
			menu_printf();
			formtail();
		}
		else
			break;
	}
	
	return choice;
}


int randnum(int range)
{
	int target_data;
	target_data = rand()%range+1;

	return target_data;
}

/*input'101',menu.   input'555',restart.
  input'999',quit.*/

int game(int choice, int target_data)
{
	int num;
	int min;
	int max;
	int jump;
	char ch;
	
	while (1) {

		min=RANGE_MIN;

		if (choice == 1)
			max = RANGE_FAST+1;
		if (choice == 2)
			max = RANGE_GENERAL+1;
		
		target_data = randnum(max-1);

		system("clear");

		formhead();
		game_printf(min, max);
		formtail();
		
		while (1) {	
			scanf("%d", &num);
			while ((ch = getchar()) != '\n' && ch != EOF);
			
			if (num == 555) {
				jump = 4;
				break;
			}

			if (num == 101) {
				jump = 1;
				break;
			}
			
			if (num == 999) {
				jump = 2;
				break;
			}

			if (num >= max || num <= min) {
				system("clear");
				formhead();
				printf(RED"\n\n\t\t 输入错误，谢谢！\n\n"TAIL);
				game_printf(min, max);
				formtail();	
			}
		
			if (num < max && num > target_data) {
				max = num;

				system("clear");
				formhead();
				game_printf(min, max);
				formtail();	
			}

			if (num > min && num < target_data) {
				min = num;

				system("clear");
				formhead();
				game_printf(min, max);
				formtail();	
			}

			if (num == target_data) {
				system("clear");
				formhead();
				over_printf(num);
				formtail();

				scanf("%d",&num);
				while ((ch = getchar()) != '\n' && ch != EOF);
				if (num == 555)
					jump = 4;
				else
					jump = 3;

				break;
			}
		}
		if (jump == 1 || jump == 2 || jump == 3)
			break;
	}
	return jump;
}

/*input'1',quit.     input whatever,menu.*/

int end(void)
{
	int over_choice;
	char ch;

	system("clear");
	formhead();
	printf(GREEN"\n\n\n\t\t 游戏结束！\n\n\n\n"TAIL);
	printf("\t 1.输入1退出游戏\n\t 2.输入任意键回到主菜单\n\n\n");
	formtail();

	scanf("%d", &over_choice);
	while ((ch = getchar()) != '\n' && ch != EOF);

	return over_choice;
}

void ruler(void)
{
	char ch;
	
	system("clear");
	formhead();
	printf(PURPLE"\n\n\n\t\t  游戏规则：\n\n\t\t 自己上网查 \n\n\n\n"TAIL);
	printf("\t\t 输入任意键返回主菜单 \n\n\n");
	formtail();
	getchar();
	getchar();
	while ((ch = getchar()) != '\n' && ch != EOF);
}


static void formhead(void)
{
	printf(GREEN "*****************************************************\n" TAIL);
	printf(PURPLE "*^_^ ^_^ ^_^ ^_^ ^_^ ^_^ ^_^ ^_^ ^_^ ^_^ ^_^ ^_^ ^_^*\n" TAIL);
}

static void formtail(void)
{
	printf(PURPLE"*^_^ ^_^ ^_^ ^_^ ^_^ ^_^ ^_^ ^_^ ^_^ ^_^ ^_^ ^_^ ^_^*\n" TAIL);
	printf(GREEN "*****************************************************\n" TAIL);
}

static void menu_printf(void)
{
	printf(PURPLE"\n\n\n\t\t  1.快速模式 \n\t\t  2.正常模式 \n\t\t  3.规则 \n\t\t  4.退出游戏 \n\n\n"TAIL);
	printf(GREEN"\t  输入1或2再按下回车键选择游戏难度 \n\n"TAIL);
}

static void game_printf(int min, int max)
{
	printf("\n\t\t 当前目标数为%d~%d之间的一个数\n\t\t 请根据范围输入你猜测的目标数\n\n\t\t 输入101返回主菜单\n\t输入999退出游戏\t输入555重启游戏\n",min,max);
}

static void over_printf(int num)
{
	printf("\n\t\t 恭喜！你猜中了！目标数就是%d\n\t\t 输入任意键进入下一步\n\n\t\t 若想再玩一次输入555\n",num);
}

static void login_menu_printf(void)
{
	printf(PURPLE"\n\n\n\t\t  1.注册账户 \n\t\t  2.登陆游戏 \n\t\t  3.退出游戏 \n\n"TAIL);
}

static void write_account(struct user user_data)
{
	FILE *fw_login = fopen("user.txt", "a+");

	fprintf(fw_login, "%s\t%s\n", user_data.account, user_data.password);
	fclose(fw_login);
}
